package com.serviceprovider.business.module.order.services.dao.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.order.model.OrderStatus;
import com.serviceprovider.business.module.order.services.dao.interfaces.IOrderStatusDao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

//~--- JDK imports ------------------------------------------------------------

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("orderStatusDao")
public class OrderStatusDaoImpl extends JdbcDaoSupport implements IOrderStatusDao {
    @Autowired
    public OrderStatusDaoImpl(@Qualifier("jdbcTemplate") JdbcTemplate jdbcTemplate) {
        setJdbcTemplate(jdbcTemplate);
    }

    @Override
    public List<OrderStatus> findAll() {
        String sql = "select * from order_status";

        return getJdbcTemplate().query(sql, new OrderStatusMapper());
    }

    @Override
    public void remove(Integer id) {
        String sql = "delete from order_status where id=?";

        getJdbcTemplate().update(sql, new Object[] { id });
    }

    @Override
    public void update(OrderStatus status) {
        String sql = "update order_status set name=?, meaning=?,active=? where id=?";

        getJdbcTemplate().update(sql, new Object[] { status.getName(), status.getMeaning(), status.isActive(),
                status.getId() });
    }

    @Override
    public void create(OrderStatus status) {
        SimpleJdbcInsert simpleJdbcInsert =
            new SimpleJdbcInsert(getJdbcTemplate()).withTableName("order_status").usingGeneratedKeyColumns("id");
        Map<String, Object> parameters = new HashMap<String, Object>();

        parameters.put("name", status.getName());
        parameters.put("meaning", status.getMeaning());
        parameters.put("active", status.isActive());

        Number id = simpleJdbcInsert.executeAndReturnKey(parameters);

        status.setId(id.intValue());
    }

    public OrderStatus get(Integer id) {
        String sql = "select * from order_status where id=" + id;

        return (OrderStatus) getJdbcTemplate().queryForObject(sql, new OrderStatusMapper());
    }

    class OrderStatusMapper implements RowMapper {
        @Override
        public Object mapRow(ResultSet resultSet, int i) throws SQLException {
            OrderStatus result = new OrderStatus();

            result.setId(resultSet.getInt("id"));
            result.setActive(resultSet.getBoolean("active"));
            result.setMeaning(resultSet.getString("meaning"));
            result.setName(resultSet.getString("name"));

            return result;
        }
    }
}



